package com.common.excption;

import com.entity.RespObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description: 全局异常处理
 * @author: GuoTong
 * @createTime: 2021-06-28 16:22
 * @since JDK 1.8 OR 11
 **/
@RestControllerAdvice
public class GlobalExceptionHander {

    private ObjectMapper objectMapper = new ObjectMapper();
    private final int NO_LOGIN_CODE = 401;

    @ExceptionHandler(value = Exception.class)
    public RespObject exceptionHandler(Exception e) {
        e.printStackTrace();
        return RespObject.respNo(e.getMessage());
    }

    @ExceptionHandler(value = MyLoginException.class)
    public RespObject MyLoginException(Exception e) {
        e.printStackTrace();
        String message = e.getMessage();
        if (String.valueOf(NO_LOGIN_CODE).equals(message)) {
            return RespObject.respOk(NO_LOGIN_CODE, e.getMessage());
        }
        return RespObject.respNo(e.getMessage());
    }


}
